package com.bw.zg3park2212.news

import android.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.lifecycleScope
import androidx.paging.filter
import androidx.paging.flatMap
import androidx.paging.map
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.NewsAddParam
import com.bw.base.entity.NewsEntityItem
import com.bw.base.entity.RepairAddParam
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.NewsAdpter
import com.bw.zg3park2212.adpter.NewsSearchAdpter
import com.bw.zg3park2212.databinding.ActivityCultureBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/app/CultureActivity")
class CultureActivity : BaseActivity<ActivityCultureBinding,NewsViewModel>() {
    lateinit var newsAdpter: NewsAdpter
    lateinit var newsSearchAdpter: NewsSearchAdpter

    override fun intitView() {
        newsAdpter= NewsAdpter()
        binding.rv.layoutManager=LinearLayoutManager(this)
        binding.rv.adapter=newsAdpter

        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    newsSearchAdpter= NewsSearchAdpter()
                    binding.rv.adapter=newsSearchAdpter
                    viewmodel.newssearch(query)
                }else{
                    lifecycleScope.launch {
                        viewmodel.getnews().collect{
                            newsAdpter.submitData(it)
                        }
                    }
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {

                return false
            }
        })
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/app/NewsAddActivity")
                .navigation()
        }
        val popupWindow= PopupWindow(this)
        val inflate = LayoutInflater.from(this).inflate(R.layout.item_pop_newsup, null)
        newsAdpter.setOnItemClickListener(object:NewsAdpter.OnItemClickListener{
            override fun onItemClick(item: NewsEntityItem, position: Int) {
                val btnYes = inflate.findViewById<Button>(R.id.btn_yes)
                val btnNo = inflate.findViewById<Button>(R.id.btn_no)
                val name1 = inflate.findViewById<EditText>(R.id.newsup_name)
                val title1 = inflate.findViewById<EditText>(R.id.newsup_title)
                btnYes.setOnClickListener {
                    val newsAddParam= NewsAddParam()
                    newsAddParam.content=item.content
                    newsAddParam.title=title1.text.toString()
                    newsAddParam.type=item.type
                    newsAddParam.publisher=name1.text.toString()
                    newsAddParam.publishDate=item.publishDate
                    viewmodel.newsup(item.id,newsAddParam)
                }
                btnNo.setOnClickListener {
                    popupWindow.dismiss()
                }
                popupWindow.isFocusable=true
                popupWindow.isOutsideTouchable=true
                popupWindow.width= ActionBar.LayoutParams.MATCH_PARENT
                popupWindow.height=600
                popupWindow.contentView=inflate
                popupWindow.showAtLocation(binding.rv, Gravity.BOTTOM,0,0)
            }
        })
        newsAdpter.setOnItemLongClickListener(object:NewsAdpter.OnItemLongClickListener{
            override fun onItemLongClick(item: NewsEntityItem, position: Int) {
                viewmodel.newdel(item.id)
                newsAdpter.notifyItemRemoved(position)
            }
        })
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/app/NewsAddActivity").navigation()
        }
    }

    override fun intitData() {
        viewmodel.newsdel.observe(this){
            ToastUtils.showLong("删除成功")
        }
        viewmodel.newssearchSuccse.observe(this){
            newsSearchAdpter.data.clear()
            newsSearchAdpter.addData(it)
            newsSearchAdpter.notifyDataSetChanged()
            ToastUtils.showLong("搜索成功")
        }
        viewmodel.newsupSuccse.observe(this){
            lifecycleScope.launch {
                viewmodel.getnews().collect{
                    newsAdpter.submitData(it)
                }
            }
            ToastUtils.showLong("修改成功")
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            viewmodel.getnews().collect{
                newsAdpter.submitData(it)
            }
        }
    }

}